package com.bjsdzk.common.utils;

import com.bjsdzk.common.constants.TimeRangeConstants;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 胜顶智控平台管理
 * 时间工具类
 *
 * @author YangTang
 * @time 2019/6/28.
 */
public class DateUtils {

    public static String getCurrMonthStr() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        return sdf.format(new Date());
    }

    public static String getLastMonthStr() {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.DAY_OF_MONTH, 1);
        c.add(Calendar.MONTH, -1);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        return sdf.format(c.getTime());
    }

    public static Date getQueryStart(long date) {
        return TimeUtils.mongoBuild(date);
    }

    public static Date getQueryEnd(long date) {
        Calendar c1 = Calendar.getInstance();
        c1.setTimeInMillis(date);
        c1.set(Calendar.HOUR_OF_DAY, 23);
        c1.set(Calendar.MINUTE, 59);
        c1.set(Calendar.SECOND, 59);
        c1.add(Calendar.HOUR_OF_DAY, 8);
        return c1.getTime();
    }

    public static Date getCurrDate() {

        Calendar calendar = Calendar.getInstance();
        return calendar.getTime();
    }

    public static Date getCurrMongoDate() {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.HOUR_OF_DAY, 8);
        return c.getTime();
    }

    public static long getCurrDayStartTime() {
        Calendar calendar = Calendar.getInstance();
        int currYear = calendar.get(Calendar.YEAR);
        int currMonth = calendar.get(Calendar.MONTH);
        int currDay = calendar.get(Calendar.DAY_OF_MONTH);
        Calendar c = Calendar.getInstance();
        c.set(currYear, currMonth, currDay, 0, 0, 0);
        return c.getTimeInMillis();
    }

    public static long getCurrDayEndTime() {
        Calendar calendar = Calendar.getInstance();
        int currYear = calendar.get(Calendar.YEAR);
        int currMonth = calendar.get(Calendar.MONTH);
        int currDay = calendar.get(Calendar.DAY_OF_MONTH);
        Calendar c = Calendar.getInstance();
        c.set(currYear, currMonth, currDay, 23, 59, 59);
        return c.getTimeInMillis();
    }

    public static Date getCurrDayDate() {
        Calendar calendar = Calendar.getInstance();
        int currYear = calendar.get(Calendar.YEAR);
        int currMonth = calendar.get(Calendar.MONTH);
        int currDay = calendar.get(Calendar.DAY_OF_MONTH);
        Calendar c = Calendar.getInstance();
        c.set(currYear, currMonth, currDay, 0, 0, 0);
        c.add(Calendar.HOUR_OF_DAY, 8);
        return c.getTime();
    }

    public static Date getCurrDayDate(long time) {
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(time);
        c.add(Calendar.HOUR_OF_DAY, 8);
        return c.getTime();
    }

    public static Date getLastDayDate() {
        Calendar calendar = Calendar.getInstance();
        int currYear = calendar.get(Calendar.YEAR);
        int currMonth = calendar.get(Calendar.MONTH);
        int currDay = calendar.get(Calendar.DAY_OF_MONTH);
        Calendar c = Calendar.getInstance();
        c.set(currYear, currMonth, currDay - 1, 0, 0, 0);
        c.add(Calendar.HOUR_OF_DAY, 8);
        return c.getTime();
    }

    public static Date getLastDayDate(long time) {
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(time);
        c.add(Calendar.DAY_OF_MONTH, -1);
        c.add(Calendar.HOUR_OF_DAY, 8);
        return c.getTime();
    }

    public static Date getBefLastDayDate(long time) {
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(time);
        c.add(Calendar.DAY_OF_MONTH, -2);
        c.add(Calendar.HOUR_OF_DAY, 8);
        return c.getTime();
    }

    public static Date getCurrMonthDate() {
        Calendar calendar = Calendar.getInstance();
        int currYear = calendar.get(Calendar.YEAR);
        int currMonth = calendar.get(Calendar.MONTH);
        int currDay = calendar.get(Calendar.DAY_OF_MONTH);
        Calendar c = Calendar.getInstance();
        c.set(currYear, currMonth, 1, 0, 0, 0);
        c.add(Calendar.HOUR_OF_DAY, 8);
        return c.getTime();
    }

    public static Date getCurrMonthDate(long time) {
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(time);
        c.set(Calendar.DAY_OF_MONTH, 1);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.add(Calendar.HOUR_OF_DAY, 8);
        return c.getTime();
    }

    public static Date getLastMonthDate(long time) {
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(time);
        c.set(Calendar.DAY_OF_MONTH, 1);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.add(Calendar.MONTH, -1);
        c.add(Calendar.HOUR_OF_DAY, 8);
        return c.getTime();
    }

    public static Date getSmlmDate(long time) {
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(time);
        c.add(Calendar.MONTH, -1);
        c.add(Calendar.HOUR_OF_DAY, 8);
        return c.getTime();
    }

    public static Date getLastMonthEnd() {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.DAY_OF_MONTH, 1);
        c.set(Calendar.HOUR_OF_DAY, 23);
        c.set(Calendar.MINUTE, 59);
        c.set(Calendar.SECOND, 59);
        c.add(Calendar.DAY_OF_MONTH, -1);
        c.add(Calendar.HOUR_OF_DAY, 8);
        return c.getTime();
    }

    public static Date getBefLastMonthStart() {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.DAY_OF_MONTH, 1);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.add(Calendar.MONTH, -2);
        c.add(Calendar.HOUR_OF_DAY, 8);
        return c.getTime();
    }

    public static Date getBefLastMonthEnd() {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.DAY_OF_MONTH, 1);
        c.set(Calendar.HOUR_OF_DAY, 23);
        c.set(Calendar.MINUTE, 59);
        c.set(Calendar.SECOND, 59);
        c.add(Calendar.MONTH, -1);
        c.add(Calendar.DAY_OF_MONTH, -1);
        c.add(Calendar.HOUR_OF_DAY, 8);
        return c.getTime();
    }

    public static Date getLastMonthDate() {
        Calendar calendar = Calendar.getInstance();
        int currYear = calendar.get(Calendar.YEAR);
        int currMonth = calendar.get(Calendar.MONTH);
        int currDay = calendar.get(Calendar.DAY_OF_MONTH);
        Calendar c = Calendar.getInstance();
        c.set(currYear, currMonth - 1, 1, 0, 0, 0);
        c.add(Calendar.HOUR_OF_DAY, 8);
        return c.getTime();
    }

    public static Date getTimeToDate(long time) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(time);
        return calendar.getTime();
    }

    public static String getTimeToStr(long time) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(time);
        return sdf.format(calendar.getTime());
    }

    public static boolean isGttoDayDate(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTimeInMillis() <= date.getTime();
    }

    public static int differDay(long createTime, long currTime) {
        long between_days = (currTime - createTime) / (1000 * 3600 * 24);
        return Integer.parseInt(String.valueOf(between_days));
    }

    public static List<String> getDayByLastWeek() {
        List<String> days = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        int d = calendar.get(Calendar.DAY_OF_WEEK);
        if (d == 1) d = 8;
        calendar.add(Calendar.DATE, -(d - 2 + 7));
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        days.add(sdf.format(calendar.getTime()));
        int index = 1;
        do {
            calendar.add(Calendar.DATE, 1);
            days.add(sdf.format(calendar.getTime()));
            index++;
        } while (index < 7);
        return days;
    }

    public static String getFirstDayByLastWeek() {
        Calendar calendar = Calendar.getInstance();
        int d = calendar.get(Calendar.DAY_OF_WEEK);
        if (d == 1) d = 8;
        calendar.add(Calendar.DATE, -(d - 2 + 7));
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(calendar.getTime()) + " 00:00:00";
    }

    public static Date getFirstDayByLastWeekWithMongo(int t) {
        Calendar calendar = Calendar.getInstance();
        int d = calendar.get(Calendar.DAY_OF_WEEK);
        if (d == 1) d = 8;
        calendar.add(Calendar.DATE, -(d - 2 + 7 * t));
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        calendar.add(Calendar.HOUR_OF_DAY, 8);
        return calendar.getTime();
    }

    public static String getLastDayByLastWeek() {
        Calendar calendar = Calendar.getInstance();
        int d = calendar.get(Calendar.DAY_OF_WEEK);
        if (d == 1) d = 8;
        calendar.add(Calendar.DATE, -(d - 1));
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(calendar.getTime()) + " 23:59:59";
    }

    public static Date getLastDayByLastWeekWithMongo(int t) {
        Calendar calendar = Calendar.getInstance();
        int d = calendar.get(Calendar.DAY_OF_WEEK);
        if (d == 1) d = 8;
        calendar.add(Calendar.DATE, -(d - 1 + 7 * (t - 1)));
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.add(Calendar.HOUR_OF_DAY, 8);
        return calendar.getTime();
    }

    public static String convertMongoDate(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.HOUR_OF_DAY, -8);
        return sdf.format(calendar.getTime());
    }

    /*
     * 获取查询mongo的时间，可区分时间段和正常时间
     * */
    public static List<Date> getMongoQueryDateExp(String date) {
        List<Date> dlist = new ArrayList<>();
        //首先判断是否为时间段，如果是时间段，则拼接出前后时间
        String[] dateRange = date.split("~");
        if (dateRange.length > 1) {
            //说明是时间段
            String startDate = dateRange[0];
            String endDate = dateRange[1];
            Date start = getDateByYMD(startDate, true, true);
            Date end = getDateByYMD(endDate, false, true);
            dlist.add(start);
            dlist.add(end);
        } else {
            String[] ds = date.split("-");
            if (ds.length == 2) {
                return getDateByYM(date, true);
            } else if (ds.length == 3) {
                Date start = getDateByYMD(date, true, true);
                Date end = getDateByYMD(date, false, true);
                dlist.add(start);
                dlist.add(end);
            }
        }

        return dlist;
    }

    public static TimeRangeConstants getMongoQueryDate(String date) {
        int year = TimeUtils.getYear(date);
        int month = TimeUtils.getMonth(date);
        int day = 1;
        int maxDay = 30;
        boolean isM = TimeUtils.isMonth(date);
        if (isM) {
            //获取当前月份的总天数
            maxDay = TimeUtils.getActualMaximum(date);
        } else {
            //按天查询
            day = TimeUtils.getDay(date);
        }

        Calendar startCa = CalendarUtils.build(year, month, day, 8, 0, 0, 0);

        Calendar end = CalendarUtils.build(year, month, isM ? maxDay : day, 23, 59, 59, 999);
        Date endD = CalendarUtils.addHours(end.getTime(), 8);

        return new TimeRangeConstants(startCa.getTime(), endD);
    }

    public static List<String> getYesterdayToNowWithHours() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar yc = Calendar.getInstance();
        yc.set(Calendar.HOUR_OF_DAY, 0);
        yc.set(Calendar.MINUTE, 0);
        yc.set(Calendar.SECOND, 0);
        yc.add(Calendar.DAY_OF_MONTH, -1);

        Calendar nc = Calendar.getInstance();
        List<String> hs = new ArrayList<>();
        while (yc.getTimeInMillis() <= nc.getTimeInMillis()) {
            hs.add(sdf.format(yc.getTime()));
            yc.add(Calendar.HOUR_OF_DAY, 1);
        }
        return hs;
    }

    public static Date getEnd(String date, Integer level) {
        if (level == 1) {
            String[] dateStr = date.split("-");
            Calendar lt_calendar = Calendar.getInstance();
            lt_calendar.set(Integer.parseInt(dateStr[0]), Integer.parseInt(dateStr[1]) - 1, Integer.parseInt(dateStr[2]), 23, 59, 59);
            lt_calendar.add(Calendar.HOUR_OF_DAY, 8);
            return lt_calendar.getTime();

        } else if (level == 2) {
            String[] dateStr = date.split("-");
            Calendar gt_calendar = Calendar.getInstance();
            gt_calendar.set(Calendar.YEAR, Integer.parseInt(dateStr[0]));
            gt_calendar.set(Calendar.MONTH, Integer.parseInt(dateStr[1]) - 1);
            int day = gt_calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
            Calendar lt_calendar = Calendar.getInstance();
            lt_calendar.set(Integer.parseInt(dateStr[0]), Integer.parseInt(dateStr[1]) - 1, day, 23, 59, 59);
            lt_calendar.add(Calendar.HOUR_OF_DAY, 8);
            return lt_calendar.getTime();
        } else if (level == 3) {
            Calendar calendar = Calendar.getInstance();
            calendar.set(Integer.parseInt(date), 11, 31, 23, 59, 59);
            calendar.add(Calendar.HOUR_OF_DAY, 8);
            return calendar.getTime();
        }
        return null;
    }

    public static Date getStart(String date, Integer level) {
        if (level == 1) {
            String[] dateStr = date.split("-");
            Calendar gt_calendar = Calendar.getInstance();
            gt_calendar.set(Integer.parseInt(dateStr[0]), Integer.parseInt(dateStr[1]) - 1, Integer.parseInt(dateStr[2]), 0, 0, 0);
            gt_calendar.add(Calendar.HOUR_OF_DAY, 8);
            return gt_calendar.getTime();

        } else if (level == 2) {
            String[] dateStr = date.split("-");
            Calendar gt_calendar = Calendar.getInstance();
            gt_calendar.set(Integer.parseInt(dateStr[0]), Integer.parseInt(dateStr[1]) - 1, 1, 0, 0, 0);
            gt_calendar.add(Calendar.HOUR_OF_DAY, 8);
            return gt_calendar.getTime();
        } else if (level == 3) {
            Calendar calendar = Calendar.getInstance();
            calendar.set(Integer.parseInt(date), 1, 1, 0, 0, 0);
            calendar.add(Calendar.HOUR_OF_DAY, 8);
            return calendar.getTime();
        }
        return null;
    }

    public static Date getMonthFirstDay(String date) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, Integer.parseInt(date.split("-")[0]));
        calendar.set(Calendar.MONTH, Integer.parseInt(date.split("-")[1]) - 1);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    public static Date getMonthLastDay(String date) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, Integer.parseInt(date.split("-")[0]));
        calendar.set(Calendar.MONTH, Integer.parseInt(date.split("-")[1]) - 1);
        int maxDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        calendar.set(Calendar.DAY_OF_MONTH, maxDay);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        return calendar.getTime();
    }

    public static String getLastMonthStr(String date) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, Integer.parseInt(date.split("-")[0]));
        calendar.set(Calendar.MONTH, Integer.parseInt(date.split("-")[1]) - 2);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        return sdf.format(calendar.getTime());
    }

    public static TimeRangeConstants getStartEndTimeByDate(String date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String[] dateRange = date.split("~");

        Date start;
        Date end;
        if (dateRange.length > 1) {
            //说明是范围
            String startDate = dateRange[0];
            String endDate = dateRange[1];
            start = getDateByYMD(startDate, true, false);
            end = getDateByYMD(endDate, false, false);
        } else {
            String[] ymd = date.split("-");
            boolean isDay = ymd.length != 2;
            //2019-04-11
            if (isDay) {
                start = getDateByYMD(date, true, false);
                end = getDateByYMD(date, false, false);
            } else {
                List<Date> dateByYM = getDateByYM(date, false);
                start = dateByYM.get(0);
                end = dateByYM.get(1);
            }
        }

        Calendar ca = Calendar.getInstance();
        ca.setTime(start);
        ca.add(Calendar.DAY_OF_MONTH, -1);
        start = ca.getTime();

        String startTime = sdf.format(start);
        String endTime = sdf.format(end);
        return new TimeRangeConstants(startTime, endTime);
    }

    public static List<Date> getMySQLQueryDate(String date) {
        String[] ymd = date.split("-");
        boolean isDay = ymd.length != 2;
        //2019-04-11
        Calendar gtC = Calendar.getInstance();
        gtC.set(Calendar.YEAR, Integer.parseInt(ymd[0]));
        gtC.set(Calendar.MONTH, Integer.parseInt(ymd[1]) - 1);
        gtC.set(Calendar.HOUR_OF_DAY, 0);
        gtC.set(Calendar.MINUTE, 0);
        gtC.set(Calendar.SECOND, 0);
        gtC.set(Calendar.MILLISECOND, 0);

        Calendar ltC = Calendar.getInstance();
        ltC.set(Calendar.YEAR, Integer.parseInt(ymd[0]));
        ltC.set(Calendar.MONTH, Integer.parseInt(ymd[1]) - 1);

        ltC.set(Calendar.HOUR_OF_DAY, 23);
        ltC.set(Calendar.MINUTE, 59);
        ltC.set(Calendar.SECOND, 59);
        ltC.set(Calendar.MILLISECOND, 999);

        if (isDay) {
            gtC.set(Calendar.DAY_OF_MONTH, Integer.parseInt(ymd[2]));
            ltC.set(Calendar.DAY_OF_MONTH, Integer.parseInt(ymd[2]));
        } else {
            // 2019-04
            gtC.set(Calendar.DAY_OF_MONTH, 1);
            //获取最大天数
            int maxDay = gtC.getActualMaximum(Calendar.DAY_OF_MONTH);
            //判断是否是当天
            SimpleDateFormat sdfDay = new SimpleDateFormat("yyyy-MM");
            if (sdfDay.format(new Date()).equals(date)) {
                maxDay = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
            }

            ltC.set(Calendar.DAY_OF_MONTH, maxDay);
        }

        List<Date> dateRange = new ArrayList<>();
        dateRange.add(gtC.getTime());
        dateRange.add(ltC.getTime());

        return dateRange;
    }

    public static Date getYesterday(Date today) {
        Calendar instance = Calendar.getInstance();
        instance.setTime(today);
        instance.add(Calendar.DAY_OF_MONTH, -1);

        return instance.getTime();
    }

    public static Date getTomorrow(Date today) {
        Calendar instance = Calendar.getInstance();
        instance.setTime(today);
        instance.add(Calendar.DAY_OF_MONTH, 1);

        return instance.getTime();
    }

    public static Date getDateByYMD(String date, boolean isStart, boolean isMongoDate) {
        String[] dateSplit = date.split("-");
        if (dateSplit.length == 2) {
            return null;
        } else {
            Integer year = Integer.valueOf(dateSplit[0]);
            Integer month = Integer.valueOf(dateSplit[1]);
            Integer day = Integer.valueOf(dateSplit[2]);
            Calendar ca = Calendar.getInstance();
            if (isStart) {
                ca.set(year, month - 1, day, 0, 0, 0);
                ca.set(Calendar.MILLISECOND, 0);
            } else {
                ca.set(year, month - 1, day, 23, 59, 59);
                ca.set(Calendar.MILLISECOND, 999);
            }

            if (isMongoDate) {
                ca.add(Calendar.HOUR_OF_DAY, 8);
            }

            return ca.getTime();
        }
    }

    public static List<Date> getDateByYM(String date, boolean isMongoDate) {
        List<Date> ldList = new ArrayList<>();
        String[] dateSplit = date.split("-");
        if (dateSplit.length == 2) {
            Integer year = Integer.valueOf(dateSplit[0]);
            Integer month = Integer.valueOf(dateSplit[1]);
            int day = 1;
            Calendar gte = Calendar.getInstance();

            gte.set(Calendar.YEAR, year);
            gte.set(Calendar.MONTH, month - 1);
            //获取当前月份的总天数
            int maxDay = gte.getActualMaximum(Calendar.DAY_OF_MONTH);

            gte.set(year, month - 1, day, 0, 0, 0);
            gte.set(Calendar.MILLISECOND, 0);

            Calendar lte = Calendar.getInstance();

            lte.set(Calendar.YEAR, year);
            lte.set(Calendar.MONTH, month - 1);

            lte.set(year, month - 1, maxDay, 23, 59, 59);
            lte.set(Calendar.MILLISECOND, 999);

            if (isMongoDate) {
                gte.add(Calendar.HOUR_OF_DAY, 8);
                lte.add(Calendar.HOUR_OF_DAY, 8);
            }

            ldList.add(gte.getTime());
            ldList.add(lte.getTime());

            return ldList;

        } else {
            return null;
        }
    }

    public static Date getPreMonthMaxDay(int year, int month) {
        Calendar instance = Calendar.getInstance();
        instance.set(year, month - 1, 1);
        instance.add(Calendar.DAY_OF_MONTH, -1);
        return instance.getTime();
    }

    public static double dateSubstract(Date d1, Date d2) {
        try {
            long diff = d1.getTime() - d2.getTime();//这样得到的差值是微秒级别
            long days = diff / (1000 * 60 * 60 * 24);

            long hours = (diff - days * (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
            long minutes = (diff - days * (1000 * 60 * 60 * 24) - hours * (1000 * 60 * 60)) / (1000 * 60);

            double result = days * 24 + hours + minutes / (60d);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    /*
     * 根据时间段查询所有格式化日期集合
     * */
    public static List<String> getPeriodDate(String date) {
        SimpleDateFormat ymdsdf = SimpleDateFormatUtils.getYMDSDF();
        List<String> result = new ArrayList<>();

        String[] dateRange = date.split("~");
        Date start;
        Date end;
        if (dateRange.length > 1) {
            //说明是范围
            String startDate = dateRange[0];
            String endDate = dateRange[1];
            start = getDateByYMD(startDate, true, false);
            end = getDateByYMD(endDate, false, false);
        } else {
            String[] ymd = date.split("-");
            boolean isDay = ymd.length != 2;
            //2019-04-11
            if (isDay) {
                start = getDateByYMD(date, true, false);
                end = getDateByYMD(date, false, false);
            } else {
                List<Date> dateByYM = getDateByYM(date, false);
                start = dateByYM.get(0);
                end = dateByYM.get(1);
            }
        }

        Calendar startCa = Calendar.getInstance();
        startCa.setTime(start);

        Calendar endCa = Calendar.getInstance();
        endCa.setTime(end);
        endCa.set(Calendar.HOUR_OF_DAY, 0);
        endCa.set(Calendar.MINUTE, 0);
        endCa.set(Calendar.SECOND, 0);
        endCa.set(Calendar.MILLISECOND, 0);

        Date newDate = null;
        while (newDate == null || newDate.before(endCa.getTime())) {
            newDate = startCa.getTime();
            result.add(ymdsdf.format(newDate));
            startCa.add(Calendar.DAY_OF_MONTH, 1);
        }

        return result;
    }

    /**
     * 判断当前时间是否在[startTime, endTime]区间，注意时间格式要一致
     *
     * @param nowTime   当前时间
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return
     * @author jqlin
     */
    public static boolean isEffectiveDate(Date nowTime, Date startTime, Date endTime) {
        if (nowTime.getTime() == startTime.getTime()
                || nowTime.getTime() == endTime.getTime()) {
            return true;
        }

        Calendar date = Calendar.getInstance();
        date.setTime(nowTime);

        Calendar begin = Calendar.getInstance();
        begin.setTime(startTime);

        Calendar end = Calendar.getInstance();
        end.setTime(endTime);

        return date.after(begin) && date.before(end);
    }

    public static int calcSafeDays(Date createdAt) {
        int safeDay = (int) ((System.currentTimeMillis()
                - createdAt.getTime())
                / (1000 * 60 * 60 * 24));

        return safeDay;
    }

    public static void main(String[] args) throws ParseException {
        SimpleDateFormat ymdhmssdf = SimpleDateFormatUtils.getYMDHMSSDF();
        Date parse = ymdhmssdf.parse("2019-12-02 14:43:00");
        int i = calcMinutes(parse);
        System.out.println(i);
    }

    public static int calcMinutes(Date createdAt) {
        int minutes = (int) ((System.currentTimeMillis()
                - createdAt.getTime())
                / (1000 * 60));

        return minutes;
    }
}
